package com.suanfa.jiaoheng;

public class 第3课栈 {
    Node head;

    // 压入数据
    public void push(Integer value){
        Node newNode = new Node();
        newNode.value = value;
        if(head==null){
            head = newNode;
        }else{
            newNode.nextNode = head;
            head = newNode;
        }
    }

    // 弹出数据
    public Integer pop(){
        Integer result;
        if(head==null){
            return null;
        }
        result = head.value;
        head = head.nextNode;
        return result;
    }

    /**
     * 判断是否为空
     * @return
     */
    public boolean isEmpty(){
        if(head==null){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 定义节点
     */
    class Node{
        Integer value;
        Node nextNode;
    }

    public static void main(String[] args) {
        第3课栈 stack = new 第3课栈();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);

        Integer val;
        while(!stack.isEmpty()){
            System.out.println(stack.pop());
        }
    }

}
